import {defineStore} from "pinia"
import {IDeviceType} from '../until/interface'
import tool from '../until/tool'
const settingData:Array<Array<IDeviceType>> = [[
    {
        id: 1,
        name: "紫外",
        normal_low: 100,
        normal_high: 2000,
        alarm: 5000,
        tempMin: 100,
        tempAlarm: 5000
    },
    {
        id: 2,
        name: "红外",
        normal_low: -40,
        normal_high: 50,
        alarm: 150,
        tempMin: -40,
        tempAlarm: 150
    },
    {
        id: 3,
        name: "振动",
        normal_low: 0,
        normal_high: 2,
        alarm: 10,
        tempMin: 2,
        tempAlarm: 10
    },
    {
        id: 4,
        name: "温度",
        normal_low: -40,
        normal_high: 20,
        alarm: 80,
        tempMin: -40,
        tempAlarm: 80
    },
    {
        id: 5,
        name: "湿度",
        normal_low: 0,
        normal_high: 45,
        alarm: 90,
        tempMin: 0,
        tempAlarm: 90
    },
    {
        id: 6,
        name: "压力",
        normal_low: 81,
        normal_high: 94,
        alarm: 200,
        tempMin: 0,
        tempAlarm: 200
    }
],
[
    {
        id: 1,
        name: "紫外",
        normal_low: 100,
        normal_high: 2000,
        alarm: 5000,
        tempMin: 100,
        tempAlarm: 5000
    },
    {
        id: 2,
        name: "红外",
        normal_low: -40,
        normal_high: 50,
        alarm: 150,
        tempMin: -40,
        tempAlarm: 150
    },
    {
        id: 3,
        name: "振动",
        normal_low: 0,
        normal_high: 2,
        alarm: 10,
        tempMin: 2,
        tempAlarm: 10
    },
    {
        id: 4,
        name: "温度",
        normal_low: -40,
        normal_high: 20,
        alarm: 80,
        tempMin: -40,
        tempAlarm: 80
    },
    {
        id: 5,
        name: "湿度",
        normal_low: 0,
        normal_high: 45,
        alarm: 90,
        tempMin: 0,
        tempAlarm: 90
    },
    {
        id: 6,
        name: "压力",
        normal_low: 81,
        normal_high: 94,
        alarm: 200,
        tempMin: 0,
        tempAlarm: 200
    }
],
[
    {
        id: 1,
        name: "紫外",
        normal_low: 100,
        normal_high: 2000,
        alarm: 5000,
        tempMin: 100,
        tempAlarm: 5000
    },
    {
        id: 2,
        name: "红外",
        normal_low: -40,
        normal_high: 50,
        alarm: 150,
        tempMin: -40,
        tempAlarm: 150
    },
    {
        id: 3,
        name: "振动",
        normal_low: 0,
        normal_high: 2,
        alarm: 10,
        tempMin: 2,
        tempAlarm: 10
    },
    {
        id: 4,
        name: "温度",
        normal_low: -40,
        normal_high: 20,
        alarm: 80,
        tempMin: -40,
        tempAlarm: 80
    },
    {
        id: 5,
        name: "湿度",
        normal_low: 0,
        normal_high: 45,
        alarm: 90,
        tempMin: 0,
        tempAlarm: 90
    },
    {
        id: 6,
        name: "压力",
        normal_low: 81,
        normal_high: 94,
        alarm: 200,
        tempMin: 0,
        tempAlarm: 200
    }
],
[
    {
        id: 1,
        name: "紫外",
        normal_low: 100,
        normal_high: 2000,
        alarm: 5000,
        tempMin: 100,
        tempAlarm: 5000
    },
    {
        id: 2,
        name: "红外",
        normal_low: -40,
        normal_high: 50,
        alarm: 150,
        tempMin: -40,
        tempAlarm: 150
    },
    {
        id: 3,
        name: "振动",
        normal_low: 0,
        normal_high: 2,
        alarm: 10,
        tempMin: 2,
        tempAlarm: 10
    },
    {
        id: 4,
        name: "温度",
        normal_low: -40,
        normal_high: 20,
        alarm: 80,
        tempMin: -40,
        tempAlarm: 80
    },
    {
        id: 5,
        name: "湿度",
        normal_low: 0,
        normal_high: 45,
        alarm: 90,
        tempMin: 0,
        tempAlarm: 90
    },
    {
        id: 6,
        name: "压力",
        normal_low: 81,
        normal_high: 94,
        alarm: 200,
        tempMin: 0,
        tempAlarm: 200
    }
],
[
    {
        id: 1,
        name: "紫外",
        normal_low: 100,
        normal_high: 2000,
        alarm: 5000,
        tempMin: 100,
        tempAlarm: 5000
    },
    {
        id: 2,
        name: "红外",
        normal_low: -40,
        normal_high: 50,
        alarm: 150,
        tempMin: -40,
        tempAlarm: 150
    },
    {
        id: 3,
        name: "振动",
        normal_low: 0,
        normal_high: 2,
        alarm: 10,
        tempMin: 2,
        tempAlarm: 10
    },
    {
        id: 4,
        name: "温度",
        normal_low: -40,
        normal_high: 20,
        alarm: 80,
        tempMin: -40,
        tempAlarm: 80
    },
    {
        id: 5,
        name: "湿度",
        normal_low: 0,
        normal_high: 45,
        alarm: 90,
        tempMin: 0,
        tempAlarm: 90
    },
    {
        id: 6,
        name: "压力",
        normal_low: 81,
        normal_high: 94,
        alarm: 200,
        tempMin: 0,
        tempAlarm: 200
    }
],
[
    {
        id: 1,
        name: "紫外",
        normal_low: 100,
        normal_high: 2000,
        alarm: 5000,
        tempMin: 100,
        tempAlarm: 5000
    },
    {
        id: 2,
        name: "红外",
        normal_low: -40,
        normal_high: 50,
        alarm: 150,
        tempMin: -40,
        tempAlarm: 150
    },
    {
        id: 3,
        name: "振动",
        normal_low: 0,
        normal_high: 2,
        alarm: 10,
        tempMin: 2,
        tempAlarm: 10
    },
    {
        id: 4,
        name: "温度",
        normal_low: -40,
        normal_high: 20,
        alarm: 80,
        tempMin: -40,
        tempAlarm: 80
    },
    {
        id: 5,
        name: "湿度",
        normal_low: 0,
        normal_high: 45,
        alarm: 90,
        tempMin: 0,
        tempAlarm: 90
    },
    {
        id: 6,
        name: "压力",
        normal_low: 81,
        normal_high: 94,
        alarm: 200,
        tempMin: 0,
        tempAlarm: 200
    }
]]
let setting:Array<Array<IDeviceType>> = []
let currentRate:number | string = localStorage.getItem("currentRate") || 300
if (localStorage.getItem("setting")) {
    setting = JSON.parse(localStorage.getItem('setting') as string)
} else {
    localStorage.setItem("setting",JSON.stringify(settingData))
    setting = settingData
}
let userInfo = JSON.parse(localStorage.getItem("userInfo")) || null
let isLogin = userInfo? true:false
export const userStore = defineStore({
    id: "user",
    state() {
        return {
            speed: currentRate, // 用户采集数据的速率3秒钟一次
            setting,
            initNum: 0,
            total: 0,
            linkCode: "", // 连接码
            currentDate: '', // 当前时间
            isWatch: false, // 是否开启了数据监听
            isLogin, //用户是否登录
            userInfo, // 用户的信息
            deviceList: [], // 设备列表
            websocket: null, // websocket的对象
            isTimer: 0, // 判断定时器是否已经开启了
            localWebsocket: null,  // 本地websocket
        }
    },
    actions: {
        // 修改采样的速率
        changeSpeed(speed:number) {
            this.speed = speed
            localStorage.setItem("currentRate",speed)
        },
        // 修改阈值
        changeValue(option:any) {
            let index = option.id
            this.setting[option.module_id - 1][index].normal_low = option.min
            this.setting[option.module_id - 1][index].normal_high = option.max
            this.setting[option.module_id - 1][index].alarm = option.alarm
            console.log(this.setting[option.module_id - 1][index])
            localStorage.setItem("setting",JSON.stringify(this.setting))
        },
        // 计算需要导入的数据量
        computeNum(num:number) {
            this.total = num
        },
        // 初始化用户的信息
        initUser(userInfo:any) {
            this.userInfo = userInfo
            this.isLogin = true
            localStorage.setItem("userInfo",JSON.stringify(userInfo))
        },
        // 退出用户的登录
        exitUser() {
            this.isLogin = false
            this.userInfo = null
            localStorage.removeItem("userInfo")
        }
    },
    getters: {
        getCurrentDate():string {
            return this.currentDate
        }
    }
})